%function negSVMscore = computeSVMrect(r, im, W, xMean)
% given image im, weight vector w and rec
function negSVMscore = computeSVMrect( r, im, W, xMean, rstart)

OUTOFRANGE = ...
    r(1) < 1 | r(2)<1 | (r(2)+r(4))>size(im,1) | (r(1)+r(3)) > size(im,2);
    
if OUTOFRANGE
    fprintf(',');
    negSVMscore = 10000;
else
    %fprintf('.');
    hog = makeHOGandRECT(im,r);
    
    % normalize hog
    hog = hog(:);
    hog = hog-xMean;
    hog(end+1,:) = 1;
    
    negSVMscore = -W'*hog;
    
    if rand(1,1)<0.1
        clf
        imagesc(im);
        rtmp = r;
        
        rectangle('Position',rtmp, 'LineWidth', 3, 'EdgeColor', 'green');
        title(negSVMscore);
        %disp(r)
        pause(0.01);
    end
    
    rectDiff = abs(r - rstart);
    rectDiff(rectDiff<4) = 4;
    rectDiff = rectDiff-4;
    diffErr = sum(rectDiff.^2 ./ 500);
    
    % light error for rects too different + a little random nudge.
    negSVMscore = negSVMscore + diffErr;  % + rand(1,1).*0.001;
    %disp(negSVMscore)
end
